#include "Frame/states.hpp"

using namespace std;

States::States()
{
    Default();

}

void States::Default()
{
    RunningFlag = false;
    PausingFlag = false;
    PreFrameFlag = false;
    SelectObjectFlag = false;
    EventChangedFlag = false;

    TrackObject.push_back(0);
    ObjectFlag = 0;
    selection.push_back(I_Rect(-1,-1,0,0));
    selection[0].color[0] = 255;
    selection[0].color[1] = 255;
    selection[0].color[2] = 0;
}

void States::Stop()
{
    RunningFlag = false;
}

void States::Pause()
{
    PausingFlag = !PausingFlag;
}

void States::PreFrame()
{
    if(PausingFlag)
        PreFrameFlag = !PreFrameFlag;
}

void States::OnMouse(Mouse_Event event, int x, int y)
{
    if(!PausingFlag)
        return;

    if( SelectObjectFlag )
    {

        selection[ObjectFlag] = I_Rect(min(x, origin.x) + std::abs(x - origin.x)/2, min(y, origin.y) + std::abs(y - origin.y)/2, std::abs(x - origin.x), std::abs(y - origin.y));
        /*
            selection &= Rect(0, 0, image.cols, image.rows);
        */
    }
    bool r_run = false;
    int r_flag = -1;

    switch(event)
    {
    case L_Button_Down:
        origin = I_Point(x, y);

        r_run = false;
        r_flag = -1;
        for(size_t i =0; i<selection.size(); i++)
        {
            if(selection[i].IsInside(origin))
            {
                if(r_run)
                    break;
                else
                {
                    r_run = true;
                    r_flag = i;
                }
            }
        }
        if(r_run)
            ObjectFlag = r_flag;
        else
        {
            selection[ObjectFlag] = (I_Rect(x,y,0,0));
            SelectObjectFlag = true;
        }
        break;
    case L_Button_Up:
        SelectObjectFlag = false;
        if( selection[ObjectFlag].width > 0 && selection[ObjectFlag].height > 0 )
        {
            TrackObject[ObjectFlag] = -1;
            //Log("Mouse worked!");
        }
        break;
    case R_Button_Down:
        break;
    case R_Button_Up:
        ObjectFlag = (ObjectFlag +1)%selection.size();
        break;
    case N_Button:
        break;
    }
}

void States::OnKey(Rect_Event event)
{

    if(EventChangedFlag)
    {
        switch(event)
        {
        case States::L_Move:
            selection[ObjectFlag].x -= 3;
            if(selection[ObjectFlag].x<0)
                selection[ObjectFlag].x=0;
            origin.x -= 3;
            if(origin.x<0)
                origin.x=0;
            break;
        case States::R_Move:
            selection[ObjectFlag].x += 3;
            origin.x += 3;
            break;
        case States::U_Move:
            selection[ObjectFlag].y -= 3;
            if(selection[ObjectFlag].y<0)
                selection[ObjectFlag].y=0;
            origin.y -= 3;
            if(origin.y<0)
                origin.y=0;
            break;
        case States::D_Move:
            selection[ObjectFlag].y += 3;
            origin.y += 3;
            break;
        case States::L_Size:
            if(selection[ObjectFlag].x - selection[ObjectFlag].width/2  <0)
                selection[ObjectFlag].x = selection[ObjectFlag].width/2 ;
            selection[ObjectFlag].width *= 1.05;
            if(selection[ObjectFlag].y - selection[ObjectFlag].height/2 <0)
                selection[ObjectFlag].y = selection[ObjectFlag].height/2;
            selection[ObjectFlag].height *= 1.05;
            break;
        case States::S_Size:
            selection[ObjectFlag].width /= 1.05;
            selection[ObjectFlag].height /= 1.05;
            break;
        case States::R_Size:
            selection[ObjectFlag].width = 108;
            selection[ObjectFlag].height = 120;
            break;
         case States::N_Size:
            //selection[ObjectFlag].x = -1;
            //selection[ObjectFlag].y = -1;
            selection[ObjectFlag].width = 0;
            selection[ObjectFlag].height = 0;
            break;
        case States::N_Object:
            selection.push_back(I_Rect(-1,-1,0,0));
            ObjectFlag = selection.size()-1;
            break;
        default:
            break;
        }
        if(selection.size()>TrackObject.size())
        {
            //int t_i = 0;
            TrackObject.push_back(0);
        }
        else
            TrackObject[ObjectFlag] = -1;
    }
    EventChangedFlag = false;
}
